<template>
  <view class="content">
    <order v-for="(item, index) in data.list" :key="index" :item="item"></order>
  </view>
</template>

<script lang="ts" setup>
import { reactive } from 'vue';
import { onLoad, onReachBottom } from '@dcloudio/uni-app';
import { err, toast } from '@/utils/uniapi';
import { api } from '@/api/mop_api';
const data = reactive({
  list: [],
  page: 1,
  size: 20,
  pageTotal: 0,
  board: 30,
  zhuolist: []
});
onLoad(() => {
  ordlist();
});
async function ordlist() {
  try {
    const { page, size } = data;
    const res = await api.orderlist({ page, size });
    if (res.respCode === 200) {
      const list = JSON.parse(res.source);
	  console.log(list)
      data.pageTotal = res.pageTotal;
      data.list = data.list.length === 0 ? list : data.list.concat(list);
      zhuolist();
    }
  } catch (error) {
    err(error);
    throw error;
  }
}
async function zhuolist() {
  try {
    const res: any = api.board_list({ page: 1, size: 50 });
    if (res.respCode === 200) {
      const zhuolist = JSON.parse(res.source);
      data.zhuolist = zhuolist;
      zhuo();
    } else {
      toast(res.msg);
    }
  } catch (error) {
    err(error);
    throw error;
  }
}
function zhuo() {
  try {
    const { list, zhuolist } = data;
    const zhuoMap = new Map(zhuolist.map((v) => [v.id, v.markNo]));
    list.forEach((e: any) => {
      const markNo = zhuoMap.get(e.deskId);
      if (markNo) {
        e.deskNo = markNo;
      }
    });
    data.list = list;
  } catch (error) {
    err(error);
    throw error;
  }
}
onReachBottom(() => {
  const { list, pageTotal, page } = data;
  if (list.length < pageTotal) {
    data.page = page + 1;
    ordlist();
  } else {
    toast('已加载全部数据');
  }
});
</script>

<style></style>
